package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.CameraMapper;
import com.one.group.admin.mapper.EquipmentMapper;
import com.one.group.model.dto.CameraAddDto;
import com.one.group.model.dto.CameraQueryDto;
import com.one.group.model.dto.CameraUpdateDto;
import com.one.group.model.entity.Camera;
import com.one.group.model.entity.Equipment;
import com.one.group.model.vo.CameraVo;
import com.one.group.service.CameraService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 52
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@Service
public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> implements CameraService {

    @Resource
    private CameraMapper cameraMapper;
    @Resource
    private EquipmentMapper equipmentMapper;

    @Override
    public List<CameraVo> findCameraByQueryDto(CameraQueryDto cameraQueryDto) {
        cameraQueryDto.setTenantId(Convert.toLong(StpUtil.getExtra("tenant_id")));
        return cameraMapper.findCameraByQueryDto(cameraQueryDto);
    }

    @Override
    @Transactional
    public boolean addCameraByAddDto(CameraAddDto cameraAddDto) {
        // 创建设备对象
        Equipment equipment = Equipment.builder()
                .tenantId(Convert.toLong(StpUtil.getExtra("adminTenantId:")))
                .deviceName(cameraAddDto.getDeviceName())
                .deviceCode(cameraAddDto.getDeviceCode())
                .deviceType("摄像头")
                .deviceMaterial("无")
                .unit("台")
                .deviceWeight("0.1")
                .price(new BigDecimal("5"))
                .deviceStock(1)
                .installAddress(cameraAddDto.getInstallAddress())
                .manufacturer(cameraAddDto.getManufacturer())
                .manufacturerAddress(cameraAddDto.getManufacturerAddress())
                .manufacturerPhone(cameraAddDto.getManufacturerPhone())
                .responsiblePerson(cameraAddDto.getResponsiblePerson())
                .build();

        // 保存设备信息
        boolean equipmentSaved = equipmentMapper.insertEquipment(equipment);
        if (!equipmentSaved) {
            return false;
        }

        // 创建摄像头对象
        Camera camera = Camera.builder()
                .equipmentId(equipment.getId())
                .address(cameraAddDto.getAddress())
                .maintenancePeriod(cameraAddDto.getMaintenancePeriod())
                .orientation(cameraAddDto.getOrientation())
                .resolution(cameraAddDto.getResolution())
                .memory(cameraAddDto.getMemory())
                .ssdCapacity(cameraAddDto.getSsdCapacity())
                .cpu(cameraAddDto.getCpu())
                .build();

        // 保存摄像头信息
        return cameraMapper.insert(camera) > 0;
    }

    @Override
    public boolean updateCameraByUpdateDto(CameraUpdateDto cameraUpdateDto) {
        TenantManager.ignoreTenantCondition();
        Camera camera = Camera.builder().build();
        BeanUtils.copyProperties(cameraUpdateDto, camera);
        return cameraMapper.update(camera) > 0;
    }

    @Override
    public boolean deleteCameraByIds(Long id) {
        return cameraMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteCameraWithMantenanceByIds(List<Long> ids) {
        ids.forEach(id -> {
            cameraMapper.deleteById(id);
        });
        return true;
    }
}
